package com.jp.knowledge.view;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiayen.dialog.LoadingDialog;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.InfoTopicListActivity;
import com.jp.knowledge.activity.LoginActivity;
import com.jp.knowledge.activity.ReadDocActivity;
import com.jp.knowledge.activity.TeamCircleCreateActivity;
import com.jp.knowledge.activity.WebBrowserActivity;
import com.jp.knowledge.comm.JpApplication;
import com.jp.knowledge.dialog.CommDialog;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.DocBean;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.InfoData;
import com.jp.knowledge.model.InfoDetailStateModel;
import com.jp.knowledge.model.TopicListInfo;
import com.jp.knowledge.my.activity.OrganizeActivity;
import com.jp.knowledge.my.model.OrganizaModel;
import com.jp.knowledge.util.L;
import com.jp.knowledge.comm.InfoDetailData;
import com.jp.knowledge.util.ResultSubscriber;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.Log;
import com.umeng.socialize.utils.ShareBoardlistener;

import java.net.URLEncoder;
import java.util.ArrayList;

/**
 * Created by 528787067@qq.com on 2017/1/7.
 */
public class TalkBottomBarView extends RelativeLayout implements View.OnClickListener, ResultSubscriber.OnResultListener {

    public static final int COMMIT_COMMENT_CODE = 0x01;        // 提交评论
    public static final int COLLECTION_CODE = 0x02;            // 收藏
    public static final int CANCEL_COLLECTION_CODE = 0x03;    // 取消收藏
    public static final int PRAISE_CODE = 0x04;                // 点赞
    public static final int PAY_REWARD = 0x05;                 // 打赏

    protected View contentView;
    protected Context context;
    protected JpApplication application;
    private InputMethodManager inputManager;
    private ClipboardManager clipboardManager;
    protected TextWatcher textWatcher;

    protected InfoDetailData infoDetailData;
    protected LoadingDialog loadingDialog;

    protected TextView editTextBtn;
    protected TextView readDocBtn;
    protected ImageView iconCommentImg;
    protected TextView iconCommentText;
    protected ImageView iconCollectionImg;
    protected ImageView iconBrokeImg;
    protected TextView iconPraiseText;
    protected ImageView iconPraiseImg;
    protected ImageView iconPayRewardImg;
    protected ImageView iconShareImg;

    protected RelativeLayout editLayout;
    protected EditText editContent;
    protected TextView cancelBtn;
    protected TextView sendBtn;
    protected TextView sendRemindText;
    protected View shadeView;             // 键盘弹起后的遮罩

    private CommDialog switchOrganizeDialog;

    /***
     * 评论提交dialog
     */
    private CommDialog    commentCommit;

    public TalkBottomBarView(Context context) {
        super(context);
        init(context);
    }

    public TalkBottomBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TalkBottomBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        initData(context);
        initDialog();
        initView();
        updateView();
    }

    public  EditText getEditContent(){
        return editContent;
    }

    /**
     * 用于设置显示样式，该方法最好手动调用，否则将使用默认样式
     * 0 代表普通样式
     * 1 代表评论详情样式
     * 2 代表文档样式
     * */
    public void setContentViewStyle(int style){
        if(contentView == null)
            return;
        if(style == 0){
            contentView.findViewById(R.id.edit_text_btn).setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.read_doc_btn).setVisibility(View.GONE);
            contentView.findViewById(R.id.detail_view).setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.icon_comment_view).setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.topic_view).setVisibility(View.GONE);
        } else if(style == 1){
            contentView.findViewById(R.id.edit_text_btn).setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.read_doc_btn).setVisibility(View.GONE);
            contentView.findViewById(R.id.detail_view).setVisibility(View.GONE);
            contentView.findViewById(R.id.topic_view).setVisibility(View.VISIBLE);
        } else if(style == 3 || style == 4){
            contentView.findViewById(R.id.edit_text_btn).setVisibility(View.GONE);
            contentView.findViewById(R.id.read_doc_btn).setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.detail_view).setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.icon_comment_view).setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.topic_view).setVisibility(View.GONE);
            if(style == 4) {
                readDocBtn.setText("工具网址");
            } else {
                readDocBtn.setText("打开文档");
            }
        } else{
            contentView.findViewById(R.id.edit_text_btn).setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.read_doc_btn).setVisibility(View.GONE);
            contentView.findViewById(R.id.detail_view).setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.icon_comment_view).setVisibility(View.GONE);
            contentView.findViewById(R.id.topic_view).setVisibility(View.GONE);
            contentView.findViewById(R.id.icon_broke_view).setVisibility(View.GONE);
        }
        contentView.findViewById(R.id.icon_broke_view).setVisibility(View.GONE);
        iconPayRewardImg.setVisibility(View.GONE);
        updateView();
    }

    private void initData(Context context){
        this.context = context;
        this.application = (JpApplication) ((Activity)context).getApplication();
        this.inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        this.clipboardManager = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
        this.infoDetailData = InfoDetailData.getInfoDetail();
    }

    private void initDialog(){
        this.loadingDialog = new LoadingDialog(context);
        this.switchOrganizeDialog = new CommDialog(context);
        this.switchOrganizeDialog.setLeft("取消");
        this.switchOrganizeDialog.setRight("确定");
        this.switchOrganizeDialog.setRightListener(this);

        commentCommit = new CommDialog(context);
        commentCommit.setRightListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editContent.setText("");
                ((Activity)context).finish();
            }
        });

        commentCommit.setLeftListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                commentCommit.dismiss();
            }
        });
        commentCommit.setContent("你有内容未发表,是否关闭?");
        commentCommit.setLeft("否");
        commentCommit.setRight("是");
    }

    public void setShadeView(View shadeView) {
        this.shadeView = shadeView;
        this.shadeView.setClickable(true);
        this.shadeView.setOnClickListener(this);
    }

    private void initView(){
        this.contentView = View.inflate(context, R.layout.talk_bottom_bar, this);
//        this.addView(contentView);

        editTextBtn = (TextView) contentView.findViewById(R.id.edit_text_btn);
        readDocBtn = (TextView) contentView.findViewById(R.id.read_doc_btn);
        iconCommentImg = (ImageView) contentView.findViewById(R.id.icon_comment_img);
        iconCommentText = (TextView) contentView.findViewById(R.id.icon_comment_text);
        iconCollectionImg = (ImageView) contentView.findViewById(R.id.icon_collection_img);
        iconBrokeImg = (ImageView) contentView.findViewById(R.id.icon_broke_img);
        iconPraiseText = (TextView) contentView.findViewById(R.id.icon_praise_text);
        iconPraiseImg = (ImageView) contentView.findViewById(R.id.icon_praise_img);
        iconPayRewardImg = (ImageView) contentView.findViewById(R.id.icon_pay_reward_img);
        iconShareImg = (ImageView) contentView.findViewById(R.id.icon_share_img);
        editLayout = (RelativeLayout) contentView.findViewById(R.id.edit_layout);
        editContent = (EditText) contentView.findViewById(R.id.edit_content);
        cancelBtn = (TextView) contentView.findViewById(R.id.cancel_btn);
        sendBtn = (TextView) contentView.findViewById(R.id.send_btn);
        sendRemindText = (TextView) contentView.findViewById(R.id.send_remind_tv);

        editTextBtn.setOnClickListener(this);
        readDocBtn.setOnClickListener(this);
        iconCommentImg.setOnClickListener(this);
        iconCollectionImg.setOnClickListener(this);
        iconBrokeImg.setOnClickListener(this);
        iconPraiseImg.setOnClickListener(this);
        iconPayRewardImg.setOnClickListener(this);
        iconShareImg.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        sendBtn.setOnClickListener(this);
        textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {};
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {};
            @Override
            public void afterTextChanged(Editable s) {
//                if(infoDetailData.getTopicInfo() != null && infoDetailData.getTopicInfo().getDiscuss() > 0){
//                    sendRemindText.setVisibility(View.GONE);
//                    sendBtn.setTextColor(getResources().getColor(R.color.font_black_think));
//                    sendBtn.setClickable(true);
//                    return;
//                }
//                if(editContent.getText().length() < 10){
//                    if(sendRemindText.getVisibility() == View.GONE)
//                        sendRemindText.setVisibility(View.VISIBLE);
//                    sendRemindText.setText(String.format("(%d/10)", editContent.getText().length()));
//                    sendBtn.setTextColor(getResources().getColor(R.color.font_gray_think));
//                    sendBtn.setClickable(false);
//                } else {
//                    sendRemindText.setVisibility(View.GONE);
//                    sendBtn.setTextColor(getResources().getColor(R.color.font_black_think));
//                    sendBtn.setClickable(true);
//                }

                if(s.toString().trim().length() == 0) {
                    sendBtn.setEnabled(false);
                    sendBtn.setTextColor(getResources().getColor(R.color.font_gray_think));
                } else {
                    sendBtn.setEnabled(true);
                    sendBtn.setTextColor(getResources().getColor(R.color.font_black_think));
                }
            }
        };
        editContent.addTextChangedListener(textWatcher);
    }

    public View getContentView(){
        return this.contentView;
    }

    public void updateView(){
//        TopicInfo topicInfo = infoDetailData.getTopicInfo();
//        if(topicInfo == null)
//            return;
//        if(topicInfo.getDiscuss() == 0){
//            editTextBtn.setText("快抢开此话题");
//            iconCommentText.setVisibility(View.GONE);
//            iconCommentImg.setImageResource(R.mipmap.comment);
//            sendBtn.setClickable(false);
//        } else{
//            editTextBtn.setText("话题等你来畅谈...");
//            iconCommentImg.setImageResource(R.mipmap.comment_on);
//            iconCommentText.setVisibility(View.VISIBLE);
//            iconCommentText.setText(topicInfo.getDiscuss() + "");
//            editContent.setHint("请输入你想要发表的内容");
//            sendRemindText.setVisibility(View.GONE);
//            sendBtn.setTextColor(getResources().getColor(R.color.font_black_think));
//            sendBtn.setClickable(true);
//        }
//        if (topicInfo.getIsCollection() == 1
//                && application.getUserData() != null
//                && application.getUserData().getIsLogin() == 1)
//            iconCollectionImg.setImageResource(R.mipmap.red_star);
//        else
//            iconCollectionImg.setImageResource(R.mipmap.stars);
//        if(topicInfo.getIsPraise() == 1
//                && application.getUserData() != null
//                && application.getUserData().getIsLogin() == 1) {
//            iconPraiseImg.setImageResource(R.mipmap.soliddz);
//            iconPraiseText.setTextColor(context.getResources().getColor(R.color.font_orign));
//        } else {
//            iconPraiseImg.setImageResource(R.mipmap.like_a);
//            iconPraiseText.setTextColor(context.getResources().getColor(R.color.font_black_think));
//        }
//        iconPraiseText.setText(topicInfo.getPraiseNum()+"");

        InfoDetailStateModel infoDetailStateModel = infoDetailData.getInfoDetailStateModel();
        if(infoDetailStateModel.getIsFollow() == 1)
            iconCollectionImg.setImageResource(R.mipmap.red_star);
        else
            iconCollectionImg.setImageResource(R.mipmap.stars);
        if(infoDetailStateModel.getCommentCount() == 0) {
            iconCommentText.setVisibility(View.GONE);
            iconCommentImg.setImageResource(R.mipmap.comment);
        } else{
            iconCommentText.setVisibility(View.VISIBLE);
            iconCommentImg.setImageResource(R.mipmap.comment_on);
            iconCommentText.setText(infoDetailStateModel.getCommentCount() + "");
        }
    }

    @Override
    public void onClick(View v) {
        if(v == this.shadeView){
            hideSoftAndEdit(false);
            return;
        }
        switch (v.getId()){
            case R.id.edit_text_btn:
                if(onCommentBtnClickListener != null)
                    onCommentBtnClickListener.onCommentBtnClick(v);
                else
                    showSoftAndEdit();
                break;
            case R.id.read_doc_btn:
                if("工具网址".equals(readDocBtn.getText())){
                    if(onCommentBtnClickListener != null)
                        onCommentBtnClickListener.onCommentBtnClick(v);
                } else {
                    readDoc();
                }
                break;
            case R.id.icon_comment_img:
                gotoComment();
                break;
            case R.id.icon_collection_img:
                collectionHead();
                break;
            case R.id.icon_broke_img:
                gotoBroke();
                break;
            case R.id.icon_praise_img:
                praise();
                break;
            case R.id.icon_pay_reward_img:
                payReward();
                break;
            case R.id.icon_share_img:
                share();
                break;
            case R.id.cancel_btn:
                hideSoftAndEdit(false);
                break;
            case R.id.send_btn:
                commitComment();
                break;
            case R.id.right_btn:
                context.startActivity(new Intent(context, OrganizeActivity.class));
                switchOrganizeDialog.dismiss();
                break;
        }
    }

    /* 显示评论框 */
    public void showSoftAndEdit(){
        editLayout.setVisibility(View.VISIBLE);
        editContent.requestFocus();
        inputManager.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
        if(shadeView != null)
            shadeView.setVisibility(View.VISIBLE);
    }

    /* 隐藏评论框 */
    public void hideSoftAndEdit(boolean flag){
//        String content = editContent.getEditableText().toString();
//        if(!TextUtils.isEmpty(content)&&content.trim().length()>0&&!flag){
//            commentCommit.show();
//        }else{
//            if(shadeView != null)
//                shadeView.setVisibility(View.GONE);
//            if (editLayout.getVisibility() == View.VISIBLE) {
//                editLayout.setVisibility(View.GONE);
//                if (inputManager != null  && inputManager.isActive())
//                    inputManager.hideSoftInputFromWindow(editContent.getWindowToken(), 0);
//            }
//        }
        if(shadeView != null)
            shadeView.setVisibility(View.GONE);
        if (editLayout.getVisibility() == View.VISIBLE) {
            editLayout.setVisibility(View.GONE);
            if (inputManager != null  && inputManager.isActive())
                inputManager.hideSoftInputFromWindow(editContent.getWindowToken(), 0);
        }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
            if (platform.name().equals("WEIXIN_FAVORITE")) {
                ToasUtil.toast(context, platform + "收藏成功啦");
            } else {
                ToasUtil.toast(context, platform + "分享成功啦");
            }
        }
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToasUtil.toast(context, platform + "分享失败啦");
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToasUtil.toast(context, platform + "分享取消了");
        }
    };

    /* 阅读文档 */
    public void readDoc(){
        Intent intent = new Intent(context,ReadDocActivity.class);
        InfoData infoData = infoDetailData.getInfoData();
        DocBean docBean = new DocBean();
        docBean.setBaiduCode(infoData.getBaiduCode());
        docBean.setHost(infoData.getHost());
        docBean.setToken(infoData.getToken());
        docBean.setFormat(infoData.getFormat());
        docBean.setPdfUrl(infoData.getQiniuUrl());
        intent.putExtra("doc", docBean);

        String pdfUrl = infoData.getQiniuUrl();
        if(pdfUrl == null || pdfUrl.length() == 0){
            ToasUtil.toast(context, "抱歉，文档链接不存在");
            return;
        }
        pdfUrl = pdfUrl.substring(0, pdfUrl.length()-3);
        ArrayList<String> qiniuUrls = new ArrayList<>();
        for(int i = 1, pageCount = infoData.getPageCount(); i <= pageCount; i++){
            qiniuUrls.add(pdfUrl + i + ".jpg");
        }
//        if(qiniuUrls.size() == 0)
//            ToasUtil.toast(context, "抱歉，该文档为空文档");
//        else
//            OpenDocActivity.startActivity(context, 0, qiniuUrls, docBean);
        try {
//            WebBrowserActivity.openWebActivity(context,
//                    "http://dcsapi.com?k=" + "113584176" + "&url=" + URLEncoder.encode(docBean.getPdfUrl(),"utf-8"),
//                    infoData.getTitle());

            WebBrowserActivity.openWebActivity(context,docBean.getPdfUrl());
        }catch (Exception e){
            L.e(e.getMessage());
        }

    }

    /* 分享 */
    protected void share(){
        if(this.onShareBtnClickListener != null)
            this.onShareBtnClickListener.onShareBtnClick(iconShareImg);
//        final InfoData infoData = infoDetailData.getInfoData();
//        new ShareAction((Activity) context)
//            .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
//            .addButton("share_copy_url", "share_copy_url", "ttoutiaofuzhilianjie", "ttoutiaofuzhilianjie")
//            .setShareboardclickCallback(new ShareBoardlistener() {
//                @Override
//                public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
//                    L.e("ShareAction", "share_media=" + share_media);
//                    if (snsPlatform.mShowWord.equals("umeng_sharebutton_custom")) {
//                        ToasUtil.toast(context, "自定义按钮");
//                    } else if(snsPlatform.mShowWord.equals("share_copy_url")){
//                        if(infoData != null){
//                            StringBuilder copyContent = new StringBuilder();
//                            if(infoData.getTitle() != null && infoData.getTitle().trim().length() > 0)
//                                copyContent.append("【" + infoData.getTitle() + "】 ");
//                            StringBuilder url = new StringBuilder(HOST_URL);
//                            url.append("id=")
//                                    .append(infoData.getId())
//                                    .append("&type=")
//                                    .append(infoData.getType());
//                            clipboardManager.setText(copyContent.append(url));
//                            ToasUtil.toast(context, "复制成功");
//                        }else {
//                            ToasUtil.toast(context, "获取详情数据失败，没有内容可复制!");
//                        }
//                    }else {
//                        if(infoData != null) {
//                            StringBuilder url = new StringBuilder(HOST_URL);
//                            url.append("id=")
//                                    .append(infoData.getId())
//                                    .append("&type=")
//                                    .append(infoData.getType());
//                            ShareAction shareAction = new ShareAction((Activity) context)
//                                    .withText(infoData.getTitle())
//                                    .withTitle("竞品说")
//                                    .withTargetUrl(url.toString())
//                                    .setPlatform(share_media)
//                                    .setCallback(umShareListener);
//                            try {
//                                if (infoData.getInfoPic() != null && infoData.getInfoPic().trim().length() > 0)
//                                    shareAction.withMedia(new UMImage(context, infoData.getInfoPic()));
//                            } catch (Exception e){} finally {
//                                shareAction.share();
//                            }
//                        }else{
//                            ToasUtil.toast(context, "获取详情数据失败!");
//                        }
//                    }
//                }
//            }).open();
    }

    /* 提交评论 */
    protected void commitComment(){
        if(editContent.getText().toString().trim().length() == 0){
            ToasUtil.toast(context, "请输入有效内容后再提交");
            return;
        } else if(showSwitchOrganizeDialog()){
            return;
        }
        loadingDialog.show();
        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
        param.addProperty("content", editContent.getText().toString());
//        param.addProperty("id", infoDetailData.getLinkId());
        param.addProperty("type", infoDetailData.getLinkType());
        param.addProperty("infoId", infoDetailData.getLinkId());
        HttpHelper.getInstance(context).addComment(param, COMMIT_COMMENT_CODE, this);
        hideSoftAndEdit(true);
    }

    /* 爆料 */
    protected void gotoBroke(){
//        if(showSwitchOrganizeDialog()){
//            return;
//        }
        Intent intent = new Intent(context, TeamCircleCreateActivity.class);
        intent.putExtra(TeamCircleCreateActivity.INTENT_TYPE, 3);
        intent.putExtra(TeamCircleCreateActivity.INTENT_LEVEL, 1);
        intent.putExtra(TeamCircleCreateActivity.INTENT_LINK_ICON, infoDetailData.getLinkIcon());
        intent.putExtra(TeamCircleCreateActivity.INTENT_LINK_ID, infoDetailData.getLinkId());
        intent.putExtra(TeamCircleCreateActivity.INTENT_LINK_TITLE, infoDetailData.getLinkTitle());
        intent.putExtra(TeamCircleCreateActivity.INTENT_LINKK_TYPE, infoDetailData.getLinkType());
        try {
            OrganizaModel organizaModel = application.getUserData().getCompanyList().get(0);
            intent.putExtra(TeamCircleCreateActivity.COMPANY_ID, organizaModel.getCompanyId());
            intent.putExtra(TeamCircleCreateActivity.COMPANY_TYPE, organizaModel.getCompanyType());
            intent.putExtra(TeamCircleCreateActivity.COMPANY_NAME, organizaModel.getCompanyName());
        } catch (Exception e){} finally {
            context.startActivity(intent);
        }
    }

    /* 点赞 */
    public void praise(){
//        TopicInfo topicInfo = infoDetailData.getTopicInfo();
//        if (topicInfo == null
//                || topicInfo.getTopicId() == null || topicInfo.getTopicId().trim().length() == 0
//                || topicInfo.getCommentId() == null || topicInfo.getCommentId().trim().length() == 0) {
//            ToasUtil.toast(context, "抱歉，没有获取到话题数据");
//        } else if (gotoLogin()) {
//            return;
//        } else if (topicInfo.getIsPraise() == 1) {
//            ToasUtil.toast(context, "抱歉，请勿重复点赞");
//        } else {
//            JsonObject param = new JsonObject();
//            param.addProperty("token", HttpHelper.token);
//            param.addProperty("infoId", infoDetailData.getLinkId());
//            param.addProperty("topicId", topicInfo.getTopicId());
//            param.addProperty("commentId", topicInfo.getCommentId());
//            HttpHelper.getInstance(context).topicPraise(param, PRAISE_CODE, this);
//        }
    }

    /* 打赏 */
    public void payReward(){
        if(gotoLogin())
            return;
//        TopicInfo topicInfo = infoDetailData.getTopicInfo();
//        PayActivity.gotoPay(context, topicInfo.getTopicId(),
//                topicInfo.getUserId(), infoDetailData.getLinkType(),
//                topicInfo.getUserName(), topicInfo.getPortrait());
    }

    /* 跳转到评论页 */
    protected void gotoComment(){
//        Intent intent = new Intent(context, InfoTopicActivity.class);
        Intent intent = new Intent(context, InfoTopicListActivity.class);
        context.startActivity(intent);
//        if(context instanceof Activity) {
//            ((Activity) context).overridePendingTransition(R.anim.topic_activity_anim_enter,
//                    R.anim.topic_activity_anim_exit);
//        }
    }

    /* 收藏 */
    protected void collectionHead(){

        if(!application.isLogin()) {
            ToasUtil.toast(context, "请登录后重试");
            context.startActivity(new Intent(context, LoginActivity.class));
            return;
        }
//        if (gotoLogin())
//            return;
        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
        param.addProperty("id", infoDetailData.getLinkId());
        param.addProperty("type", infoDetailData.getLinkType());
//        if (infoDetailData.getTopicInfo().getIsCollection() == 1) {
        if (infoDetailData.getInfoDetailStateModel().getIsFollow() == 1) {
            HttpHelper.getInstance(context).cancleCollectionHead(param, CANCEL_COLLECTION_CODE, this);
        } else {
            HttpHelper.getInstance(context).collectionHead(param, COLLECTION_CODE, this);
        }
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {
        loadingDialog.cancel();
        updateView();
        if(this.talkBottomBarCallback != null)
            this.talkBottomBarCallback.viewNeedToUpdate(requestType, true);
    }
    @Override
    public void onError(int requestType) {
//        if(requestType == COMMIT_COMMENT_CODE) {
//            if(infoDetailData.getTopicInfo().getDiscuss() == 0)
//                ToasUtil.toast(context, "抱歉，话题提交失败，请稍后重试");
//            else
//                ToasUtil.toast(context, "抱歉，评论失败，请稍后重试");
//        } else if(requestType == COLLECTION_CODE) {
//            ToasUtil.toast(context, "抱歉，收藏失败，请稍后重试");
//        } else if(requestType == CANCEL_COLLECTION_CODE){
//            ToasUtil.toast(context, "抱歉，取消收藏失败，请稍后重试");
//        } else if(requestType == PRAISE_CODE){
//            ToasUtil.toast(context, "抱歉，点赞失败，请稍后重试");
//        } else if(requestType == PAY_REWARD){
//
//        }
        onCompleted(requestType);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            ToasUtil.toast(context, data.getMessage());
            onError(requestType);
            return;
        }
//        TopicInfo topicInfo = infoDetailData.getTopicInfo();
//        switch (requestType){
//            case COMMIT_COMMENT_CODE:           // 提交评论
//                if(topicInfo.getDiscuss() == 0)
//                    ToasUtil.toast(context, "话题提交成功");
//                else
//                    ToasUtil.toast(context, "评论成功");
//                topicInfo.setDiscuss(topicInfo.getDiscuss()+1);
//                editContent.setText("");
//                if(topicInfo.getTopicId() != null && topicInfo.getTopicId().trim().length() > 0){
//                    List<TopicListInfo> topicList = infoDetailData.getTopicList();
//                    if(topicList == null)
//                        topicList = new ArrayList<>();
//                    TopicListInfo topicListInfo = data.getEntity(TopicListInfo.class);
//                    if(topicListInfo.getInfoId() == null || topicListInfo.getInfoId().length() == 0)
//                        topicListInfo.setInfoId(infoDetailData.getLinkId());
//                    topicList.add(0, topicListInfo);
//                }
//                break;
//            case COLLECTION_CODE:               // 收藏
//                topicInfo.setIsCollection(1);
//                ToasUtil.toast(context, "收藏成功");
//                break;
//            case CANCEL_COLLECTION_CODE:        // 取消收藏
//                topicInfo.setIsCollection(0);
//                ToasUtil.toast(context, "取消收藏成功");
//                break;
//            case PRAISE_CODE:                       // 点赞
//                topicInfo.setIsPraise(1);
//                topicInfo.setPraiseNum(topicInfo.getPraiseNum()+1);
//                break;
//            case PAY_REWARD:                        // 打赏
//                break;
//        }
        InfoDetailStateModel infoDetailStateModel = infoDetailData.getInfoDetailStateModel();
        switch (requestType){
            case COMMIT_COMMENT_CODE:           // 提交评论
                ToasUtil.toast(context, "评论成功");
                infoDetailStateModel.setCommentCount(infoDetailStateModel.getCommentCount()+1);
                editContent.setText("");
                if(commentSuccessCallback != null) {
                    TopicListInfo topicListInfo = data.getEntity(TopicListInfo.class);
                    commentSuccessCallback.commentSuccess(topicListInfo);
                }
                break;
            case COLLECTION_CODE:               // 收藏
                infoDetailStateModel.setIsFollow(1);
//                ToasUtil.toast(context, "收藏成功");
                if(collectionListener != null)
                    collectionListener.onCollection(true);
                break;
            case CANCEL_COLLECTION_CODE:        // 取消收藏
                infoDetailStateModel.setIsFollow(0);
//                ToasUtil.toast(context, "取消收藏成功");
                if(collectionListener != null)
                    collectionListener.onCollection(false);
                break;
            case PRAISE_CODE:                       // 点赞
                break;
            case PAY_REWARD:                        // 打赏
                break;
        }
    }

    /* 跳转到登录界面（现在不用登陆也可以操作了） */
    protected boolean gotoLogin(){
//        UserData userData = application.getUserData();
//        if(userData == null || userData.getIsLogin() != 1
//                || userData.getUuid().trim().length() == 0) {
//            ToasUtil.toast(context, "请登录后重试");
//            context.startActivity(new Intent(context, LoginActivity.class));
//            return true;
//        }
        return false;
    }

    /* 组织没通过审核弹出对话框（现在没有限制了） */
    protected boolean showSwitchOrganizeDialog(){
//        if(gotoLogin())
//            return true;
//        UserData userData = application.getUserData();
//        if(userData.getCompanyId() == null || userData.getCompanyId().trim().length() == 0) {
//            switchOrganizeDialog.setContent("你当前还未加入任何组织，请加入组织后重试");
//            switchOrganizeDialog.show();
//            return true;
//        }
//        if(userData.getAuditStete() != 1){
//            switchOrganizeDialog.setContent("你所在的组织未通过审核，暂时不支持该操作，是否切换到其他组织？");
//            switchOrganizeDialog.show();
//            return true;
//        }
        return false;
    }

    private TalkBottomBarCallback talkBottomBarCallback;
    public void setTalkBottomBarCallback(TalkBottomBarCallback callback){
        this.talkBottomBarCallback = callback;
    }
    public interface TalkBottomBarCallback{
        // 需要更新界面是通知更新数据
        void viewNeedToUpdate(int requestType, boolean isSuccess);
    }

    private CommentSuccessCallback commentSuccessCallback;
    public void setCommentSuccessCallback(CommentSuccessCallback commentSuccessCallback){
        this.commentSuccessCallback = commentSuccessCallback;
    }
    public interface CommentSuccessCallback{
        void commentSuccess(TopicListInfo data);
    }

    public boolean  activityFinish(){
        String content = editContent.getEditableText().toString();
//        if(!TextUtils.isEmpty(content)&&content.trim().length()>0) {
//            commentCommit.show();
//            return  false;
//        }
        return  true;
    }

    private OnShareBtnClickListener onShareBtnClickListener;
    public void setOnShareBtnClickListener(OnShareBtnClickListener onShareBtnClickListener){
        this.onShareBtnClickListener = onShareBtnClickListener;
    }
    public interface OnShareBtnClickListener{
        void onShareBtnClick(View shareBtn);
    }
    private OnCommentBtnClickListener onCommentBtnClickListener;
    public void setOnCommentBtnClickListener(OnCommentBtnClickListener onCommentBtnClickListener){
        this.onCommentBtnClickListener = onCommentBtnClickListener;
    }
    public interface OnCommentBtnClickListener{
        void onCommentBtnClick(View view);
    }
    private CollectionListener collectionListener;
    public void setCollectionListener(CollectionListener collectionListener){
        this.collectionListener = collectionListener;
    }
    public interface CollectionListener{
        void onCollection(boolean isCollection);
    }
}
